﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.NetworkInformation;
namespace WindowsFormsApplication1
{
    public partial class Stock : Form
    {
        public Stock()
        {
            InitializeComponent();
        }

        public static bool NetStatus()
        {
            return NetworkInterface.GetIsNetworkAvailable();
        }


        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (NetStatus() == false)
                {
                    MessageBox.Show("No internet Connection!");
                }
                else
                {
                    try
                    {
                        //getDataView();
                        graph1.clearChart();
                        graph1.ticker = textBox1.Text ;
                        graph1.ambilData();
                        graph1.tampil();
                        graph1.tampil2();
                        double status = (graph1.Close[0] - graph1.Close[1]) / graph1.Close[1] * 100;
                       // MessageBox.Show(graph1.RSI[0].ToString());
                        tampiltabel();
                        if (status < 0)
                        {
                           //MessageBox.Show("turun " + status.ToSting() + "%");
                            label3.Text = "down " + status.ToString() + "%";
                            pictureBox1.Image = imageList1.Images[0];
                            
                        }
                        else
                        {
                           //essageBox.Show("naik " + status.ToString() + "%");
                            label3.Text = "up " + status.ToString() + "%";
                            pictureBox1.Image = imageList1.Images[1];
                        }
                        if (graph1.prediction() =="stagnant")
                        {
                            pictureBox2.Image = imageList1.Images[2];
                            label5.Text = "tommorow will likely same as today";
                        }
                        else if (graph1.prediction() == "down")
                        {
                            pictureBox2.Image = imageList1.Images[0];
                            label5.Text = "tommorow will likely to down  fast, sell it now";
                        }
                        else
                        {
                            pictureBox2.Image = imageList1.Images[1];
                            label5.Text = "tommorow will likely to up fast,buy it now";
                        }

                    }
                    catch (WebException f)
                    {
                        MessageBox.Show("Stock Not Found");
                    }
                }
               
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
         
        }

        public void tampiltabel()
        {
            DataTable dt = new DataTable("stock");
            dt.Columns.Add("Date", typeof(DateTime));
            dt.Columns.Add("Open", typeof(string));
            dt.Columns.Add("Close", typeof(string));
            dt.Columns.Add("High", typeof(string));
            dt.Columns.Add("Low", typeof(string));
           
            //MessageBox.Show(graph1.Open[0].ToString());
            for (int i = 0; i < 6; i++)
            {
                dt.Rows.Add(new Object[] { graph1.date[i], graph1.Open[i].ToString(), graph1.Close[i].ToString(),graph1.High[i],graph1.Low[i]
            });
            }
            
        
            DataView dv = dt.DefaultView;
            //return dv;
            dataGridView1.DataSource = dv;
        }

        

        private void search_Click(object sender, EventArgs e)
        {
            if (NetStatus() == false)
            {
                MessageBox.Show("No internet Connection!");
            }
            else
            {
                try
                {
                   
                    graph1.clearChart();
                    graph1.ticker = textBox1.Text + ".JK";
                    graph1.ambilData();
                    graph1.tampil();
                    graph1.tampil2();
                    double status = (graph1.Close[0] - graph1.Close[1]) / graph1.Close[1] * 100;
                    // MessageBox.Show(graph1.RSI[0].ToString());
                    tampiltabel();
                    if (status < 0)
                    {
                        //MessageBox.Show("turun " + status.ToSting() + "%");
                        label3.Text = "down " + status.ToString() + "%";
                        pictureBox1.Image = imageList1.Images[0];
                        
                    }
                    else
                    {
                        //essageBox.Show("naik " + status.ToString() + "%");
                        label3.Text = "up " + status.ToString() + "%";
                        pictureBox1.Image = imageList1.Images[1];
                        
                    }
                    if (graph1.RSI[0] < 80 && graph1.RSI[0] > 20)
                    {
                        pictureBox2.Image = imageList1.Images[2];
                        label5.Text = "tommorow will likely same as today";
                    }
                    else if (graph1.RSI[0] > 80)
                    {
                        pictureBox2.Image = imageList1.Images[0];
                        label5.Text = "tommorow will likely to down  fast, sell it now";
                    }
                    else
                    {
                        pictureBox2.Image = imageList1.Images[1];
                        label5.Text = "tommorow will likely to up fast,buy it now";
                    }

                }
                catch (WebException f)
                {
                    MessageBox.Show("Stock Not Found");
                }
            }
        }

        private void label6_Click(object sender, EventArgs e)
        {

        }
    }
}
